<template>
  <div class="liveRoomPage">
    <nav class="formSearch">
      <el-form :inline="true" :model="listQuery">
        <el-form-item label="状态:" label-width="100px">
          <el-radio-group v-model="listQuery.state" @change="searchTypeChange">
            <el-radio-button
              v-for="item in searchType"
              :key="item.value"
              :label="item.value"
            >
              {{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <br />
        <el-form-item label="关键字:" label-width="100px">
          <el-input
            v-model="listQuery.search"
            maxlength="20"
            placeholder="请输入商品名称/ID"
            class="inputKeyWord"
            style="width: 400px"
          >
            <template #append>
              <el-button :icon="Search" @click="getList" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="margin-left: 20px" type="success" @click="addPro">添加直播商品</el-button>
        </el-form-item>
      </el-form>
    </nav>
    <section>
      <el-table
        v-loading="tableLoading"
        :data="roomList"
        border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
        tooltip-effect="dark"
        style="width: 100%"
        class="dataTable"
      >
        <el-table-column prop="id" label="直播商品ID" />
        <el-table-column prop="productName" label="商品名称" />
        <el-table-column prop="productId" label="关联商品ID" />
        <el-table-column prop="productImage" width="100" label="商品图片">
          <template #default="scope">
            <img
              style="width: 40px; height: 40px"
              :src="scope.row.productImage"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="商品小程序路径">
          <template #default="scope">
            /pages_category_page1/goodsModule/goodsDetails?productId=
            {{ scope.row.productId }}
          </template>
        </el-table-column>
        <el-table-column label="价格类型" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.priceType == 1"> 一口价 </el-tag>
            <el-tag v-if="scope.row.priceType == 2" type="success">
              价格区间
            </el-tag>
            <el-tag v-if="scope.row.priceType == 3" type="danger">
              折扣价
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.state == 0" type="warning"> 待审核 </el-tag>
            <el-tag v-if="scope.row.state == 1" type="success"> 已通过 </el-tag>
            <el-tag v-if="scope.row.state == 2" type="danger"> 未通过 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="微信审核状态" width="120">
          <template #default="scope">
            <el-tag v-if="scope.row.wxAuditStatus == 0" type="info">
              未审核
            </el-tag>
            <el-tag v-if="scope.row.wxAuditStatus == 1" type="success">
              审核中
            </el-tag>
            <el-tag v-if="scope.row.wxAuditStatus == 2" type="warning">
              审核通过
            </el-tag>
            <el-tag v-if="scope.row.wxAuditStatus == 3" type="danger">
              审核失败
            </el-tag>
            <el-tag v-if="scope.row.wxAuditStatus == -1"> 未提交 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <!-- 暂无 -->
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template #default="scope">
            <div class="btnList">
              <el-button
                v-if="scope.row.state != 1"
                type="primary"
                link
                @click="edit(scope.row)"
              >编辑</el-button>
              <el-button type="primary" link @click="seeMore(scope.row)">详情</el-button>
              <el-popconfirm
                title="确认删除？"
                @confirm="del(scope.row)"
              >
                <template #reference>
                  <el-button
                    link
                    type="danger"
                  >删除</el-button>
                </template>
              </el-popconfirm>
              <el-button
                v-if="scope.row.state == 2"
                type="primary"
                link
                @click="reExamine(scope.row)"
              >重新审核</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          background
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </section>

    <el-dialog
      v-model="DetailsShow"
      :title="'直播间商品信息'"
      width="50%"
      center
      :close-on-click-modal="false"
    >
      <el-row>
        <el-col :span="12">
          <el-form :model="ProForm" label-width="180px">
            <el-form-item label="商品名称:">
              {{ ProForm.productName }}
            </el-form-item>
            <el-form-item label="商品库存:">
              {{ ProForm.stockNumber }}
            </el-form-item>
            <el-form-item label="商品图片:">
              <img
                style="width: 120px; height: 120px"
                :src="ProForm.productImage"
                alt=""
                srcset=""
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-form :model="ProForm" label-width="180px">
            <el-form-item label="商品价格:">
              <span v-if="ProForm.priceType == 1">
                一口价: {{ ProForm.fixedPrice }}
              </span>
              <span v-if="ProForm.priceType == 2">
                最低价： {{ ProForm.minPrice }} - 最高价：{{ ProForm.maxPrice }}
              </span>
              <span v-if="ProForm.priceType == 3">
                现价: {{ ProForm.marketPrice }}
              </span>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <div v-if="ProForm.remark" style="text-align: center; margin-top: 20px">
        <span style="color: red">备注：</span>
        {{ ProForm.remark }}
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="DetailsShow = false">关 闭</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 添加直播间商品 -->
    <el-dialog
      v-model="addProShow"
      :title="proShowState == 1 ? '新建直播间商品' : '编辑直播间商品'"
      width="50%"
      center
      :close-on-click-modal="false"
    >
      <div v-if="proShowState == 1">
        <el-form :inline="true" :model="productForm">
          <el-form-item class="inputWide" label="关键字：">
            <el-input
              v-model="productForm.search"
              maxlength="20"
              placeholder="请输入内容"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plai @click="getClassify">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table
          ref="singleTable"
          border
          :data="proTableData"
          highlight-current-row
          style="width: 100%"
          @current-change="selProduct"
        >
          <el-table-column property="productId" label="商品ID" />
          <el-table-column property="productImage" label="商品主图">
            <template #default="scope">
              <img
                style="width: 40px; height: 40px"
                :src="scope.row.productImage"
                alt=""
              />
            </template>
          </el-table-column>
          <el-table-column property="productName" label="商品名称" />
          <el-table-column property="stockNumber" label="库存" />
        </el-table>
        <el-pagination
          style="margin-top: 20px"
          :current-page="productForm.page"
          :page-sizes="[5, 10, 20, 50, 100]"
          :page-size="productForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="proTotal"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
        />
      </div>
      <el-form
        style="margin-top: 20px"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item
          v-if="proShowState == 1"
          label="选中的商品"
          prop="productImage"
        >
          <div class="uploadWidth">
            <el-upload
              class="avatar-uploader"
              :headers="headers"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handlePictureCardPreview1"
              disabled
            >
              <img v-if="selImage" :src="selImage" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </div>
          <span style="color: red">* 请选择商品商品</span>
        </el-form-item>
        <!-- <el-form-item v-if="proShowState!=2 || form.wxAuditStatus == -1" label="商品封面图" prop="productImage"> -->
        <el-form-item label="商品封面图" prop="productImage">
          <div class="uploadWidth">
            <el-upload
              class="avatar-uploader"
              :headers="headers"
              :action="uploadUrl"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
              :on-success="handlePictureCardPreview1"
            >
              <img
                v-if="form.productImage"
                :src="form.productImage"
                class="avatar"
              />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </div>
          <span style="color: red">* 图片规则：图片尺寸最大300像素*300像素</span>
        </el-form-item>
        <!-- <el-form-item v-if="proShowState!=2 || form.wxAuditStatus == -1" label="商品库存" prop="stockNumber"> -->
        <el-form-item label="商品库存" prop="stockNumber">
          <el-input
            v-model="form.stockNumber"
            maxlength="9"
            style="width: 60%"
            oninput="value=value.replace(/[^\d]/g,'')"
          />
        </el-form-item>
        <!-- <el-form-item v-if="proShowState!=2 || form.wxAuditStatus == -1" label="商品名称" prop="productName"> -->
        <el-form-item label="商品名称" prop="productName">
          <el-input
            v-model="form.productName"
            maxlength="20"
            style="width: 60%"
          />
        </el-form-item>
        <el-form-item label="价格类型" prop="priceType">
          <el-radio-group v-model="form.priceType">
            <el-radio :label="1">一口价</el-radio>
            <el-radio :label="2">价格区间</el-radio>
            <el-radio :label="3">折扣价</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="价格" prop="title">
          <el-input-number
            v-if="form.priceType == 1"
            v-model="form.fixedPrice"
            :max="999999999"
            :min="0"
            :precision="2"
            :step="1"
            placeholder="一口价"
            style="width: 250px"
          />
          <el-input-number
            v-if="form.priceType == 2"
            v-model="form.minPrice"
            :max="999999999"
            :min="0"
            :precision="2"
            :step="1"
            placeholder="最低价"
            style="width: 250px"
          />
          <el-input-number
            v-if="form.priceType == 2"
            v-model="form.maxPrice"
            :max="999999999"
            :min="0"
            :precision="2"
            :step="1"
            placeholder="最高价"
            style="margin-left: 20px; width: 250px"
          />
          <el-input-number
            v-if="form.priceType == 3"
            v-model="form.marketPrice"
            :max="999999999"
            :min="0"
            :precision="2"
            :step="1"
            placeholder="折扣价"
            style="width: 250px"
          />
          <!-- <el-input
              v-if="form.priceType == 1"
              v-model="form.fixedPrice"
              placeholder="一口价"
              style="width: 160px"
            />
            <el-input
              v-if="form.priceType == 2"
              v-model="form.minPrice"
              placeholder="最低价"
              style="width: 160px"
            />
            <el-input
              v-if="form.priceType == 2"
              v-model="form.maxPrice"
              style="margin-left: 20px; width: 160px"
              placeholder="最高价"
            />
            <el-input
              v-if="form.priceType == 3"
              v-model="form.marketPrice"
              placeholder="折扣价"
              style="width: 160px"
            /> -->
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addProShow = false">取 消</el-button>
          <el-button v-if="proShowState == 1" type="success" @click="addProSubmit">添 加</el-button>
          <el-button
            v-if="proShowState == 2"
            type="primary"
            @click="editProSubmit"
          >保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onBeforeMount } from 'vue'
import { getClassifyGetAll } from '@/api/commodity'
import {
  liveProGetAll,
  delLiveProduct,
  addProductSave,
  editProductSave,
  ProReExamine,
} from '@/api/live'
import { WXuploadUrl } from '@/utils/request'
import { getToken } from '@/utils/auth'

const uploadUrl = WXuploadUrl
const headers = {
  'Authorization-business': getToken()
}
const searchType = [
  {
    label: '全部',
    value: 'null',
  },
  {
    label: '待审核',
    value: 0,
  },
  {
    label: '审核已通过',
    value: 1,
  },
  {
    label: '审核未通过',
    value: 2,
  },
]
const tableLoading = ref(false)
const listQuery = ref({
  state: null,
  search: '',
  page: 1,
  pageSize: 10,
  shopId: null
})
const proListQuery = ref({
  liveId: null,
  page: 1,
  pageSize: 10,
  shopId: null
})
// 直播间列表
const roomList = ref([])
// 直播相关产品列表
const roomProList = ref([])
const total = ref(0)
const DetailsShow = ref(false)
const auditShow = ref(false)
const activeName = ref('first')
const disabled = ref(false)
// 添加直播间
const ProForm = ref({})
// 直播间添加验证
const liveRules = ref([])
const auditForm = ref({})
const auditFormState = ref(1)
const addProShow = ref(false)
const proShowState = ref(1)
const productForm = ref({
  search: '', // 搜索字段
  shelveState: 1, // 上架状态 1-上架 0-不上架 null-全部
  stock: 1, // 库存状态 1-有库存 0-无库存 null-全部
  page: 1, // 当前页
  pageSize: 5,
})
const proTotal = ref(0)
const proTableData = ref([])
const form = ref({
  productImage: null,
  productMediaId: null,
  stockNumber: null,
  productName: null,
  priceType: 1,
  productId: null,
  total: null,
})
const rules = {
  stockNumber: [
    { required: true, message: '商品库存不能为空', trigger: 'blur' },
  ],
  productName: [
    { required: true, message: '商品名称不能为空', trigger: 'blur' },
  ],
}
// 选中的商品图片
const selImage = ref('')

onBeforeMount(() => {
  getList()
})

// 获取商品列表
const getList = () => {
  tableLoading.value = true
  liveProGetAll(listQuery.value).then(res => {
    total.value = res.data.total
    roomList.value = res.data.list
  }).finally(() => {
    tableLoading.value = false
  })
}
const seeMore = (row) => {
  ProForm.value = row
  DetailsShow.value = true
}
const del = (row) => {
  ElMessageBox.confirm('是否确认删除该内容?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    delLiveProduct(row)
      .then((res) => {
        ElMessage.success('删除成功')
        getList()
      })
      .catch((err) => {
        ElMessage.error(err.msg)
      })
  }).catch(() => {})
}
// 选择查询状态
const searchTypeChange = (e) => {
  listQuery.value.state = e
  getList()
}
// 分页
const handleCurrentChange = (e) => {
  listQuery.value.page = e
  getList()
}
const handleSizeChange = (e) => {
  listQuery.value.pageSize = e
  getList()
}
// 添加商品
const addPro = () => {
  proShowState.value = 1
  addProShow.value = true
  getClassify()
  selImage.value = ''
  form.value = {
    productImage: null,
    stockNumber: null,
    productName: null,
    priceType: 1,
    productId: null,
    total: null,
  }
}
const edit = (row) => {
  proShowState.value = 2
  addProShow.value = true
  const data = Object.assign({}, row)
  form.value = data
}
const getClassify = async () => {
  const res = await getClassifyGetAll(productForm.value)
  proTotal.value = res.data.total
  proTableData.value = res.data.list
}
const handleSizeChange1 = (e) => {
  productForm.value.pageSize = e
  getClassify()
}
const handleCurrentChange1 = (e) => {
  productForm.value.page = e
  getClassify()
}
const handlePictureCardPreview1 = (file) => {
  if (file.code === '200') {
    form.value.productImage = file.data.url
    form.value.productMediaId = file.data.mediaId
  } else {
    ElMessage.error('图片超出规定像素，上传失败！')
  }
}
const beforeAvatarUpload = async (file) => {
  const istype = file.type === 'image/jpeg' || file.type === 'image/png'
  const isLt2M = file.size / 1024 / 1024 < 2
  const isSize = await new Promise(function (resolve, reject) {
    const width = 300
    const height = 300
    const _URL = window.URL || window.webkitURL
    const image = new Image()
    image.onload = function () {
      const valid = image.width <= width && image.height <= height
      valid ? resolve() : reject()
    }
    image.src = _URL.createObjectURL(file)
  }).then(
    () => {
      return file
    },
    () => {
      ElMessage.error('上传的图片最大分辨率不超过300*300')
      return Promise.reject(false)
    }
  )
  if (!istype) {
    ElMessage.error('上传图片只能是 JPG 或 PNG格式!')
  }
  if (!isLt2M) {
    ElMessage.error('上传图片大小不能超过 2MB!')
  }
  return isSize && istype && isLt2M
}
// 选中的商品
const selProduct = (e) => {
  selImage.value = e.productImage
  form.value.productId = e.productId
  form.value.total = e.stockNumber
  form.value.stockNumber = e.stockNumber
  form.value.productImage = e.productImage
  form.value.productName = e.productName
}
const addProSubmit = () => {
  addProductSave(form.value).then((res) => {
    if (res.code === '') {
      addProShow.value = false
      getList()
      ElMessage.success('添加成功')
    }
  })
}
const editProSubmit = () => {
  editProductSave(form.value).then((res) => {
    if (res.code === '') {
      addProShow.value = false
      getList()
      ElMessage.success('编辑成功')
    }
  })
}
// 重新审核
const reExamine = (row) => {
  ProReExamine({ id: row.id }).then((res) => {
    if (res.code === '') {
      ElMessage.success('已重新提交')
      getList()
    }
  })
}
</script>

<style lang="scss" scpoed>
.liveRoomPage {
  padding: 20px;
  margin-top: 20px;
  background-color: #FFFFFF;
  nav {
    .search {
      line-height: 40px;
      margin: 10px 0;
      display: flex;
      .title {
        min-width: 120px;
        text-align: right;
      }
      .inputKeyWord {
        max-width: 400px;
        min-width: 200px;
      }
    }
  }
  section {
    padding: 12px 8px;
    background-color: #fff;
    .pagination {
      margin-top: 30px;
    }
  }
}
.search {
  line-height: 40px;
  margin: 10px 0;
  display: flex;
  .title {
    min-width: 120px;
    text-align: right;
  }
  .inputKeyWord {
    max-width: 400px;
    min-width: 200px;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #3A68F2;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
}
.avatar {
  width: 140px;
  height: 140px;
  display: block;
}
</style>
